//进行中的任务和待审核的申请公用同一个js
$(document)
		.ready(
				function() {
					// 待审核的列表的数据绑定
					var exameListVue = new Vue({
						el : '#exameListData',
						data : {
							items : null
						},
						methods : {
							//way 代表查看方式  show表示查看 exame表示审核
							exame : function(item,way) {
								openExameBox(item,way);
							}
						}
					});
					
					var way=$("#way").val();
					var option=null;
					if (way=='exame') {
						option=[{ text: '采购订单', value: 'purchaseOrder' },  
							      { text: '入库申请', value: 'inStockApply' },  
							      { text: '出库申请', value: 'outStockApply' } ];
					}else {
						option=[{ text: '采购订单', value: 'purchaseOrder'},  
						        	{ text: '退货申请', value: 'returnApply'},
							      { text: '入库申请', value: 'inStockApply'},  
							      { text: '出库申请', value: 'outStockApply'}
							      ];
					}
					
					//类型选择的数据绑定 
					var category = new Vue({
						el : '#categoryListDiv',
						data : {
							selected: 'purchaseOrder',  
						    options:option
						},
						methods : {
							search : function(option) {
								alert(option.value);
							},
							configData: function(index) {
						            alert(index);
						        }
						}
					});
					
						var category=null;
					// 待审核的数据列表
					var exameList = new Array();

					// 获得所有的待审核的list
					//getAllExameList();
					
					$("#searchCategoryButton").on("click", function() {
						category=$("#selectCategory").val();
						var way=$("#way").val();
						var url=null;
						//待审核的申请
						if (way=='exame') {
							if (category=='purchaseOrder') {
								url='purchaseOrder/getPurchaseOrderListByStateAjax?state=1';
							}else if (category=='inStockApply') {
								url='stockApply/getStockApplyByStateAndCategoryAjax?state=1&category=in';
							}else {
								url='stockApply/getStockApplyByStateAndCategoryAjax?state=1&category=out';
							}
						}
						else {
							//进行中的任务
							if (category=='purchaseOrder') {
								url='purchaseOrder/getPurchaseOrderListByFinishAjax?finish=0';
							}else if (category=='returnApply') {
								url='returnApply/getReturnApplyByFinishAjax?finish=0';
							}else if (category=='inStockApply') {
								url='stockApply/getStockApplyByFinishAndCategoryAjax?finish=0&category=in';
							}else {
								url='stockApply/getStockApplyByFinishAndCategoryAjax?finish=0&category=out';
							}
						}
						
						
						$.get(SERVER_PRE_URL +url,
								function(data) {
							exameListVue.items = data;
								});
					});
					
					
					
					
					
					
					
					

					function openExameBox(item,way) {
						var url=null;
						var title=null;
						if (category=='purchaseOrder') {
							title='采购申请';
							url='purchaseOrder/getPurchaseOrderByPrimaryKeyReturnTo?purchaseOrderId='+item.purchaseOrderId+'&remark='+way+'&returnTo=showPurchaseOrder';
						}else if(category=='returnApply'){
							title='退货申请';
							url='returnApply/getReturnApplyByPrimaryKeyReturnTo?returnApplyId='+item.returnApplyId+'&remark='+way+'&returnTo=showReturnApply';
						}
						else if(category=='inStockApply'){
							title='入库申请';
							url='stockApply/getStockApplyByPrimaryKeyReturnTo?stockApplyId='+item.stockApplyId+'&remark='+way+'&returnTo=showStockApply';
						}else{
							title='出库申请';
							url='stockApply/getStockApplyByPrimaryKeyReturnTo?stockApplyId='+item.stockApplyId+'&remark='+way+'&returnTo=showStockApply';
						}
						
						if (way=='exame') {
							exameOrder(item,url,title);
						}else {
							showOrder(item,url,title);
						}
					}
					
					//审核申请
					function exameOrder(item,url,title){
						var exameBox = layer
						.open({
							type : 2,
							maxmin : true,
							title : '审核'+title,
							offset : '4%',
							btnAlign : 'c',
							area : [ '70%', '92%' ],
							btn : [ '审核通过', '未通过' ] // 只是为了演示
							,
							yes : function() {
								// 获得子窗口的值
								var childBox = layer.getChildFrame(
										'body', exameBox);
								var exameSuggestion = childBox.find(
										"#inputExameSuggestion").val();
								
								var data =null;
								var exameUrl=null;
								if (category=='purchaseOrder') {
									data= {
											"purchaseOrderId" : item.purchaseOrderId,
											"state" : 2,
											"exameSuggestion" : exameSuggestion
										};
									exameUrl="purchaseOrder/examePurchaseOrderAjax";
								}
								else if (category=='inStockApply') {
									data= {
											"stockApplyId" : item.stockApplyId,
											"state" : 2,
											"exameSuggestion" : exameSuggestion,
											"finish":1
										};
									exameUrl="stockApply/exameInStockApplyByPrimaryKeyAjax";
								}else{
									data= {
											"stockApplyId" : item.stockApplyId,
											"state" : 3,
											"exameSuggestion" : exameSuggestion,
											"finish":1
										};
									exameUrl="stockApply/exameOutStockApplyByPrimaryKeyAjax";
								}

								baseUtil.httpSend(exameUrl,data, showExameResult);
								layer.close(exameBox);

							},
							btn2 : function() {
								// 获得子窗口的值
								var childBox = layer.getChildFrame(
										'body', exameBox);
								// 新添加的菜品
								var exameSuggestion = childBox.find(
										"#inputExameSuggestion").val();

								var data =null;
								var exameUrl=null;
								if (category=='purchaseOrder') {
									data= {
											"purchaseOrderId" : item.purchaseOrderId,
											"state" : 3,
											"exameSuggestion" : exameSuggestion
										};
									exameUrl="purchaseOrder/examePurchaseOrderAjax";
								}
								else if (category=='inStockApply') {
									data= {
											"stockApplyId" : item.stockApplyId,
											"state" : 3,
											"exameSuggestion" : exameSuggestion,
											"finish":1
										};
									exameUrl="stockApply/exameInStockApplyByPrimaryKeyAjax";
								}else{
									data= {
											"stockApplyId" : item.stockApplyId,
											"state" : 3,
											"exameSuggestion" : exameSuggestion,
											"finish":1
										};
									exameUrl="stockApply/exameOutStockApplyByPrimaryKeyAjax";
								}
								
								
								baseUtil
										.httpSend(exameUrl,data, showExameResult);
								layer.close();
							},
							shadeClose : true, // 点击遮罩关闭
							content : SERVER_PRE_URL
									+url
						});
					}
					
					function showOrder(item,url,title){
						var showBox = layer
						.open({
							type : 2,
							maxmin : true,
							title : '查看'+title,
							offset : '4%',
							btnAlign : 'c',
							area : [ '70%', '92%' ],
							shadeClose : true, // 点击遮罩关闭
							content : SERVER_PRE_URL
									+url
						});
					}
					
					//显示审核结果
					function showExameResult(result){
						if (result==1) {
							layer.msg('审核成功');
						}else {
							layer.msg('审核失败');
						}
						refreshList();
					}

					//刷新显示数据
					function refreshList() {
						var url=null;
						if (category=='purchaseOrder') {
							url="stockManage/getAllExameListAjax";
						}else if(category=='inStockApply'){
							url="stockApply/getStockApplyByStateAndCategoryAjax?state=1&category=in";
						}else {
							url="stockApply/getStockApplyByStateAndCategoryAjax?state=1&category=out";
						}
						baseUtil.httpSend(url,
								null, showData);
					}

					function showData(data) {
						exameListVue.items=data.purchaseOrderList;
						//Vue.set(exameListVue, "items", data.purchaseOrderList);
					}

				});